<?php
/********************************************************/
/* Module Name:  gosearch.php						    */
/* Description: search and retrieve the information from */
/*    database and display to the screen     			*/
/* Written by: Maria Cielo Carreno							*/
/********************************************************/

session_start();
//If your session isn't valid, it returns you to the login screen for protection
if(empty($_SESSION['myusername'])){
 header("location:main_login.php");
}
//echo $_SESSION['myusername'];
if(empty($_SESSION['seedval'])){
 header("location:main_login.php");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link href="default.css" rel="stylesheet" type="text/css" />
        <title>Search Results</title>
</head>
<body>
<div id="fulladmin">
<?php

if(isset($_POST['searching']) && $_POST['searching'] == 'yes'){
		//echo "DEBUG THIS LINE";
		//get field search
		$find_val = strip_tags($_POST['find']);
		$find_val = trim($find_val);
		
		$field_val = strip_tags($_POST['field']);
		$_SESSION['field_val'] = $field_val;
		$_SESSION['find_val'] = $find_val;
		
		switch($field_val){
			case "caseNo":
				$sql_command = "SELECT * FROM casereferred WHERE upper($field_val) LIKE'%$find_val%' ORDER BY CaseNo ASC";
				break;
			case "mediatorName":
				$sql_command = "SELECT  mediator.mediatorId,mediator.mediatorName, casereferred.caseNo, schedule.Date, casereferred.ControlNo ";
				$sql_command .= "FROM mediator LEFT JOIN casereferred ON mediator.mediatorId=casereferred.mediatorId ";
				$sql_command .= " LEFT JOIN schedule ON casereferred.caseNo = schedule.CaseNo ";
				if ($find_val == ""){
						$sql_command.= " WHERE schedule.status != 'reset'";
						//$sql_command .= "WHERE casereferred.caseStatus = 'YES' "; //AND schedule.status = 'Ongoing'";
				}
				else{
						$sql_command .= "WHERE mediator.mediatorName = '$find_val' ";
						//AND casereferred.caseStatus = 'YES' "; //
						$sql_command .= " AND schedule.status = 'Ongoing' ";
				}
				//echo $sql_command;
				break;
			case "Ndescription":
				$sql_command = "SELECT natureofcase.code,natureofcase.Ndescription,casereferred.caseNo,casereferred.dateRegister, casereferred.ControlNo FROM natureofcase INNER JOIN casereferred ON natureofcase.code=casereferred.natureOfCase WHERE upper($field_val) LIKE'%$find_val%' ORDER BY CaseNo ASC";
				break;
			case "Date":
				list($startd,$endd)=explode("-",$find_val);
				//echo $startd;
				$startd = preg_replace("/\//","-",$startd);
				$endd = preg_replace("/\//","-",$endd);
				//echo $startd;
				$sql_command = "SELECT * FROM casereferred WHERE dateRegister BETWEEN '".$startd."' AND '".$endd."'";
				break;
		}
		
		$_SESSION['sql_script'] = $sql_command; 
	}		
		
		$sql_script = $_SESSION['sql_script'];
		//echo $sql_script;
/* 
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of admin.php that includes pagination
*/

        // connect to the database
        include('connect-db.php');
        
        // number of results to show per page
        $per_page = 10;
        
        // figure out the total pages in the database
        $result = mysql_query($sql_script);
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];
                
                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }
        
        // display pagination
        echo "<div id='adminleft'>";
        echo "<p><b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='gosearch.php?page=$i'>$i</a> ";
        }
        echo "</p>";
        echo "</div>";        
        // display data in table
		echo "<div id='adminright'>";
		echo "<center><h1>Search Return</h1><br>";
        echo "<table border='1' cellpadding='10'>";
		$field_val = $_SESSION['field_val'];
		switch($field_val){
			case "caseNo":
				echo "<tr> <th>Case No</th> <th>Plaintiff Name</th> <th>Defendant Name</th> </tr>";
				break;
			case "mediatorName":
				if($find_val=="")
					echo "<tr><th> Mediator Name</th><th> Case Number </th><th> Mediation Schedule</th></tr>";
				else{
					echo "<tr><th> Mediator: $find_val </th></tr>";
					echo "<tr><th> Case Number </th><th> Mediation Schedule </th></tr>";
				}
				break;
			case "Ndescription":
				echo "<tr><th> Nature Of Case</th><th> Case Number </th><th> Date Received</th></tr>";
				break;
			case "Date":
				echo "<tr><th> Case No</th><th> Date Received</th></tr>";
				break;				
		}
	
		if($total_pages == 0){
			// close table>
			echo "</table><br>";
			echo "<center>No search found</center>";
		}
		else{
        // loop through results of database query, displaying them in the table 
			for ($i = $start; $i < $end; $i++)
			{
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }
			
				switch($field_val){
					case "caseNo":
                // echo out the contents of each row into a table
						echo "<tr>";
						echo '<td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '"> '. mysql_result($result, $i, 'CaseNo') . '</a></td>';
                //echo '<td>' . mysql_result($result, $i, 'caseTitle') . '</td>';
						echo '<td>' . mysql_result($result, $i, 'plaintiffName') . '</td>';
						echo '<td>' . mysql_result($result, $i, 'defendantName') . '</td>';
						//echo '<td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '">View details</a></strong></td>';
						echo "</tr>"; 
						break;
					case "mediatorName":
					               // echo out the contents of each row into a table
						if($find_val==""){
							echo "<tr>";
							echo '<td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '"> '. mysql_result($result, $i, 'mediatorName') . '</a></td>';
                //echo '<td>' . mysql_result($result, $i, 'caseTitle') . '</td>';
							echo '<td>' . mysql_result($result, $i, 'caseNo') . '</td>';
							echo '<td>' . mysql_result($result, $i, 'Date') . '</td>';
						//echo '<td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '">View details</a></strong></td>';
							echo "</tr>"; 
						}
						else{
							echo '<tr><td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '"> '. mysql_result($result, $i, 'caseNo') . '</a></td>';
							echo '<td>' . mysql_result($result, $i, 'Date') . '</td></center></tr>';
						}
						break;
					case "Ndescription":
					                // echo out the contents of each row into a table
						echo "<tr>";
						echo '<td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '"> '. mysql_result($result, $i, 'Ndescription') . '</a></td>';
                //echo '<td>' . mysql_result($result, $i, 'caseTitle') . '</td>';
						echo '<td>' . mysql_result($result, $i, 'caseNo') . '</td>';
						echo '<td>' . mysql_result($result, $i, 'dateRegister') . '</td>';
						//echo '<td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '">View details</a></strong></td>';
						echo "</tr>"; 
						break;
					case "Date":
					                // echo out the contents of each row into a table
						echo "<tr>";
						echo '<td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '"> '. mysql_result($result, $i, 'CaseNo') . '</a></td>';
                //echo '<td>' . mysql_result($result, $i, 'caseTitle') . '</td>';
						echo '<td>' . mysql_result($result, $i, 'dateRegister') . '</td>';
						//echo '<td><strong><a href="details.php?id=' . mysql_result($result, $i, 'ControlNo') . '">View details</a></strong></td>';
						echo "</tr>"; 
						break;

				}
			}
			// close table>
			echo "</table>"; 
		}


?>
</div>
</div>
<div id="footer"><center><a href="search_frm.php?id=<?php echo $_SESSION['seedval']; ?>"><font color="red">Search again</font></a>.Return to main <a href="dashboard.php"><font color="red">Dashboard</font></a>, or you can <a href="log_out.php?id= <?php echo $_SESSION['userid']; ?>"><font color="red">Log Out</font></a></center></div>
</body>
</html>